layui.use(['table', 'LayerForm', 'LayerAjax'], function () {
    var table = layui.table;
    var exbase = layui.exbase;
    var $ = layui.$;

    var moduleTable = table.render({
        elem: '#module_table_id',
        height: 'full-60',
        id: 'module_table_id',
        url: ctxPath + '/module/page',
        method: 'POST',
        contentType: 'application/json',
        dataType: 'json',
        page: {
            layout: ['limit', 'count', 'prev', 'page', 'next', 'skip'],
            first: false, //不显示首页
            last: false //不显示尾页
        },
        request: {
            pageName: 'current', // 页码的参数名称，默认：page
            limitName: 'size' // 每页数据条数的参数名，默认：limit
        },
        cols: [[ //表头
            {checkbox: true}
            , {field: 'id', title: 'ID', width: 80, sort: true, hide: true}
            , {field: 'moduleCode', title: '模块编码'}
            , {field: 'moduleName', title: '模块名'}
            , {
                field: 'enabled', title: '状态', templet: function (d) {
                    if (d.enabled === 1) {
                        return '<span style="color: blue">启用</span>'
                    } else {
                        return '<span style="color: pink">禁用</span>';
                    }
                }
            }
            , {field: 'createTime', title: '创建时间'}
            , {field: 'right', title: '操作', width: 134, minWidth: 125, toolbar: '#operator'}
        ]],
        parseData: function (res) {
            return {
                "code": res.errorCode,
                "msg": res.errorMsg,
                "count": res.data.total,
                "data": res.data.records
            }
        },
    });

    $("#add_button_id").on('click', function () {
        var form = new layui.LayerForm({
            url: ctxPath + "/module/saveDelayTaskModule",
            method: 'POST',
            title: '创建延时任务模板',
            contentType: 'application/json',
            success: function () {
                moduleTable.reload();
            },
            form: [{
                name: "moduleCode",
                label: "模板编码",
                type: 'text',
                verify: 'required'
            }, {
                name: "moduleName",
                label: "模板名称",
                type: 'text',
                verify: 'required'
            }]
        })
        form.open()
    });


    table.on('tool(module_table_id)', function (obj) {
        if (obj.event === 'edit') {
            edit(obj)
        }
        if (obj.event === 'forbidden') {
            enable(obj, 0);
        }
        if (obj.event === 'enabled') {
            enable(obj, 1)
        }
    });

    function edit(obj) {
        var data = obj.data; // 得到当前行数据
        var moduleId = data.id;
        var form = new layui.LayerForm({
            url: ctxPath + "/module/updateModule",
            method: 'POST',
            title: '修改用户',
            contentType: 'application/json',
            onload: {
                url: ctxPath + "/module/findModule",
                data: {"id": moduleId},
                method: 'POST'
            },
            success: function () {
                moduleTable.reload();
            },
            form: [{
                name: "moduleCode",
                label: "模板编码",
                type: 'text',
                verify: 'required'
            }, {
                name: "moduleName",
                label: "模板名称",
                type: 'text',
                verify: 'required'
            }, {
                name: 'id',
                label: 'id',
                type: 'hide'
            }]
        })
        form.open()
    }

    function enable(obj, enabled) {
        var msg;
        if (enabled === 1) {
            msg = '是否启用' + obj.data.moduleName + '模块?';
        } else {
            msg = '是否禁用' + obj.data.moduleName + '模块?';
        }
        layer.confirm(msg, function (index) {
            layer.close(index);
            // 禁用
            enableModule(obj.data.id, enabled);
        });
    }

    layui.form.on('submit(search_button_filter)', function (data) {
        var moduleName = data.field.moduleName;
        if (!moduleName || moduleName === '') {
            moduleName = null;
        }
        moduleTable.reload({
            page: {
                curr: 1,
            },
            where: {
                parameter: {
                    moduleName: moduleName,
                }
            }
        })
    });

    layui.form.on('submit(reset_button_filter)', function (data) {
        $("#module_name_input_id").val('');
        var moduleName = null;
        moduleTable.reload({
            page: {
                curr: 1,
            },
            where: {
                parameter: {
                    moduleName: moduleName,
                }
            }
        })
    });

    function enableModule(id, enabled) {
        var data = {
            "id": id,
            "enabled": enabled
        }
        var msg;
        if (enabled === 1) {
            msg = '启用';
        } else {
            msg = '禁用';
        }
        $.ajax({
            async: false,
            url: ctxPath + '/module/enabled',
            method: 'POST',
            contentType: 'application/json',
            dataType: 'json',
            data: JSON.stringify(data),
            success: function (data) {
                if (data.errorCode == exbase.AJAX_SUCCESS_CODE) {
                    moduleTable.reload();
                } else {
                    layer.msg(msg + "失败<br>" + data.errorMsg, {icon: 2});
                }
            }
        })
    }
})